﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace CrossHair
{
    public class CrossHair
    {
        public Vector2 Direction; //Can be used as direction for for bullet, so add this to bullet position each update.
        private const int WIDTH = 24;
        private const int HEIGHT = 24;
        private const float DISTANCEFROMPLAYER = 50;
        private int mouseX = 0;
        private int mouseY = 0;
        private Point position;
        private Texture2D texture;
        private Player person;
        public CrossHair(Texture2D crosshairTexture, Player person)
        {
            this.texture = crosshairTexture;
            this.person = person;
            position = new Point(person.Bounds.X + (person.Bounds.Width*3), person.Bounds.Y + (person.Bounds.Height/2));
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(this.texture, new Rectangle(this.position.X - (WIDTH / 2), this.position.Y - (HEIGHT / 2), WIDTH, HEIGHT), Color.White);
        }

        public void Update()
        {
            //First get the mouse position (X and Y)
            MouseState current_mouse = Mouse.GetState();
            mouseX = current_mouse.X;
            mouseY = current_mouse.Y;

            //Create two Vectors for the person and his crosshair
            Vector2 personVector = new Vector2(person.Bounds.X + (person.Bounds.Width / 2), person.Bounds.Y + (person.Bounds.Height / 2));
            Vector2 mouseVector = new Vector2(mouseX, mouseY);
            //Calculate delta
            Vector2 delta = mouseVector - personVector;
            //Calculate distance between middle of person and the mouse location
            float distance = Vector2.Distance(personVector, mouseVector);

            //Calculate the direction and use it to calculate a different position on that line;
            this.Direction = delta / distance;
            Vector2 newPosition = personVector + (this.Direction * DISTANCEFROMPLAYER);

            //Store new crosshair position
            this.position = new Point(Convert.ToInt32(newPosition.X), Convert.ToInt32(newPosition.Y));
        }
    }
}
